<?php

namespace app\api\controller\Account;

use app\admin\controller\KeywordsCompany;
use app\admin\model\AdminPaper;
use app\admin\model\Admin;
use app\admin\model\AdminBasicZoom;
use app\admin\model\AdminLog;
use app\admin\model\AuthGroupAccessZoom;
use app\common\controller\Api;
use app\common\controller\Callfunc;
use app\common\library\Ems;
use app\common\library\Iden;
use app\common\library\Sms;
use app\api\controller\sms as csms;
use app\admin\model\Account;
use Exception;
use fast\Random;
use think\Config;
use think\Cookie;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;
use think\Hook;
use think\Session;
use think\Validate;

/**
 * 成为举办人接口
 */
class Sponsor extends Api
{
    protected $noNeedLogin = ['index','answer','isanswer'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }
    /**
     * 成为举办人
     */
    public function index()
    {
        $uInfo = $this->auth->getUserInfo();
       // $uInfo = $this->>auth->getUserInfo();
        if(empty($uInfo)){//未登录
            $this->error(__('Please login first'), null, 401);
        }

        $this->success(__('Sign up successful'),  $uInfo,200);
    }

    /*
     * 举办人填写调查问卷
    */
    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */

    public function answer(){
//        $data = '{
//                    '1': "sdcfvbn"
//                    '2': "b"
//                    '3': ["A", "B", "C", "E", "D"]
//                }';

       // $uInfo = self::getUserInfo();
        $uInfo = $this->auth->getUserInfo();
        if(empty($uInfo)){//未登录
            $this->error(__('Please login first'), null, 401);
        }
        $admin_id = Iden::decrypt($uInfo['id']);
        $data = $this->request->param('data');
       // print_r($data);
       // echo '------';
        if(empty($data)){
            $this->error('内容不能为空或格式错误，请重试');
        }

//        $content = json_decode($data,true);//json转化为数组
//
//        if(empty($content)){
//            $this->error('内容不能为空或格式错误，请重试');
//        }
        try {
            $p = Db::name('admin_paper')->where('admin_id', '=', $admin_id)->find();
                if(empty($p)){
                    $d['admin_id'] = $admin_id;
                    $d['ctime'] = time();
                    $d['ip'] = real_ip();
                    $d['content'] = $data;
                    $paper =  new AdminPaper();//用户调查问卷
                    $r = $paper->save($d);
                    if(!$r){
                        $this->error('调查失败，请重试');
                    }
                }
            } catch (ValidateException $e) {
                echo $e->getMessage();
            } catch (PDOException $e) {

                echo $e->getMessage();
            } catch (Exception $e) {

                echo $e->getMessage();
            }

        $this->success(__('调查成功'),  $data,200);
    }

    /*
     * 判断是否做过调查问卷
     */
    public function isanswer(){
      //  $uInfo = self::getUserInfo();
        $uInfo = $this->auth->getUserInfo();
        if(empty($uInfo)){//未登录
            $this->error(__('Please login first'), null, 401);
        }
        $admin_id = $uInfo['id'];
        $p = Db::name('admin_paper')->where('admin_id', '=', $admin_id)->find();
        if(empty($p)){
            $status = 0;
        }else{
            $status = 1;
        }
        $this->success(__(''),  ['status'=>$status],200);
    }










}
